/*
 * Copyright (C) 2017-2025
 * ZKMall All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.zkmall.commons.utils;

import cn.hutool.core.util.ObjectUtil;
import com.shop.zkmall.commons.constant.CoReturnFormat;
import com.shop.zkmall.commons.exception.CoBusinessException;
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;

/**
 * 验证工具
 *
 * @author zkmall zkmallvip@163.com
 */
public class ValidationUtil {

    /**
     * 验证空
     */
    public static void isNull(Object obj, String entity, String parameter, Object value) {
        if (ObjectUtil.isNull(obj)) {
            String msg = entity + " 不存在: " + parameter + " is " + value;
        }
    }

    /**
     * 验证是否为邮箱
     */
    public static boolean isEmail(String email) {
        return new EmailValidator().isValid(email, null);
    }

    /**
     * 检测价格区间
     */
    public static boolean checkPrice(BigDecimal price, BigDecimal min, BigDecimal max) {
        if (price == null || min == null || max == null) {
            return false;
        }
        return price.compareTo(min) >= 0 && price.compareTo(max) <= 0;
    }

    public static void checkValidateCode(String key, String validateCode) {
        Environment environment = SpringUtil.getBean(Environment.class);
        boolean enableValidateCode = environment.getProperty("enable-validate-code", Boolean.class, true);
        RedisTemplate redisTemplate = (RedisTemplate) SpringUtil.getBean("redisTemplate");
        //TODO 特殊验证码,测试用
        if (enableValidateCode && "9999".equals(validateCode)) {
            return;
        }
        //手机号登录,校验验证码
        String code = (String) redisTemplate.opsForValue().get(key);
        if (!validateCode.equals(code)) {
            throw new CoBusinessException(CoReturnFormat.CODE_ERROR);
        }
    }
}
